<?php

namespace app\common\service\member;
use app\common\model\member\MemberBillModel;
use app\common\service\setting\Exception;
use DateTime;
/**
 * 账单明细
 */
class MemberBillService
{
    /**
     * 添加、修改字段
     * @var array
     */
    public static $edit_field = [
        'id'=>'',
        'member_id'=>'',
        'money'=>'',
        'principal'=>'',
        'commission'=>'',
        'principal_balance'=>'',
        'commission_balance'=>'',
        'type'=>'',
        'member_account_id'=>'',
        'data_id'=>'',
        'title'=>'',
        'remark'=>'',
    ];
    /**
     * 账单明细列表
     *
     * @param array  $where 条件
     * @param int    $page  页数
     * @param int    $limit 数量
     * @param array  $order 排序
     * @param string $field 字段
     *
     * @return array
     */
    public static function list($where = [], $page = 1, $limit = 10,  $order = [], $field = '',$month=null)
    {
        $model = new MemberBillModel();
        $pk = $model->getPk();
        if (empty($field)) {
            $field = 'id,money,principal,commission,principal_balance,commission_balance,type,member_account_id,data_id,title,remark,is_delete,create_time';
        }
        if (empty($order)) {
            $order = ['create_time'=>'desc',$pk => 'desc'];
        }
        /***************************月份处理****************************************/
        $income = 0;//收入
        $expenses = 0;//支出
        if($month){
            $datetime =new DateTime($month);
            // 获取月初的时间
            $beginningOfMonth = clone $datetime;
            $beginningOfMonth->modify('first day of this month');
            $beginningOfMonthFormatted = $beginningOfMonth->format('Y-m-d') . ' 00:00:00';

            // 获取月末的时间
            $endOfMonth = clone $datetime;
            $endOfMonth->modify('last day of this month');
            $endOfMonthFormatted = $endOfMonth->format('Y-m-d') . ' 23:59:59';
            if($month !=date('Y-m')){
                $where[] = ['create_time','>=',$beginningOfMonthFormatted];
                $where[] = ['create_time','<=',$endOfMonthFormatted];
            }
            $income = $model
                ->where('member_id',member_id())
                ->where('is_delete',0)
//                ->where('type',"<>",MemberBillModel::getType('SJFK',1))
                ->whereBetween('create_time',[$beginningOfMonthFormatted,$endOfMonthFormatted])
                ->where('money','>',0)
                ->sum('money');
            $expenses = $model
                ->where('member_id',member_id())
                ->where('is_delete',0)
                ->whereBetween('create_time',[$beginningOfMonthFormatted,$endOfMonthFormatted])
                ->where('money','<',0)
                ->sum('money');
        }
        if ($page == 0 || $limit == 0) {
            return $model->field($field)->where($where)->order($order)->select()->toArray();
        }
        $count = $model->where($where)->count();
        $pages = ceil($count / $limit);
        $list = $model->field($field)->where($where)->page($page)->limit($limit)->order($order)->select()->toArray();
        foreach ($list as $key=>$val){
            $list[$key]['balance'] = bcadd($val['principal_balance'], $val['commission_balance'],2);
        }
        return compact('count', 'pages', 'page', 'limit', 'list','income','expenses');
    }

    /**
     * 查询账单明细所需参数
     * @Author: 易军辉
     * @DateTime:2024-09-26 16:13
     * @param $member_id 买家ID
     * @return array
     */
    public static function getParams($member_id=0)
    {
        //查询账单开始年份
        $start_date = MemberBillModel::where('member_id',$member_id)->where('is_delete',0)->order(['create_time'=>'asc'])->value('create_time');
        if(!$start_date){
            $start_date = date('Y-01-01');
        }else{
            $start_date  = date('Y-m-01',strtotime($start_date));
        }
        $end_date = date('Y-12-31');
        $month = date('Y-m');
        $bill_types = MemberBillModel::TYPE;
        array_unshift($bill_types, ['value' => -1, 'label' => '全部', 'code' => 'ALL']);
        return compact('start_date','end_date','month','bill_types');
    }

}
